<?php

/**
 *
 *	@author Joshgeng <[<pengqiao.geng@zhaopin.com.cn>]>
 *	@date(2017.9.2) 创建
 * 
 */
class Comon_model extends CI_Model{

	function __construct()
	{
		$this->load->helper('date');
		$this->load->library('session');
		$this->load->library('email');
		$this->load->database();
	}

	/**
	 * 判断权限
	 * @param  [type] $value  [description]
	 * @param  [type] $status [description]
	 * @return [type]         [description]
	 */
	public function _userpower($power)
	{
		$session =  $this->session->userdata('username');
		$this->db->select('ZL_USER_GROUPID');
		$this->db->where('ZL_USER_NAME',$session);
		$zl_users = $this->db->get('zl_users');
		if ($zl_users->num_rows()<=0) {
			return  array('status' => -1);
		}

		$this->db->select('ZL_POWER_POWERID id');
		$this->db->from('zl_power a');
		$this->db->join('zl_power_group_list b','b.ZL_POWER_GROUP_LIST_POWERID = a.ZL_POWER_ID','left');
		$this->db->where('ZL_POWER_GROUP_LIST_GROUPID',$zl_users->row()->ZL_USER_GROUPID);
		$this->db->where('ZL_POWER_POWERID',$power);
		$zl_power = $this->db->get();
		if ($zl_power->num_rows()<=0) {
			return  array('status' => -1);
		}

		return  array('status' => 1);
	}

	/**
	 * 创建邮件的登录（帐号/密码）
	 * @return [type] [description]
	 */
	public function _createEmailConfig()
	{
		// 获得邮箱配置
		$email_conf =$this->config->item('email');
		$config['protocol']='smtp';
		//$config['smtp_crypto'] ='ssl';
		$config['smtp_timeout'] = 30;
		$config['smtp_port'] = 25;
		$config['crlf'] = "\r\n";
		$config['newline'] = "\r\n"; 
		$config['smtp_host']=$email_conf['host'];
		$config['smtp_user']=$email_conf['username'];
		$config['smtp_pass']=$email_conf['password'];
		$config['mailtype']='html';
		$config['charset']='utf-8';
		$this->email->initialize($config);
	}

	/**
	 * 发送邮件
	 * @param  [type] $client [description]
	 * @param  [type] $cc     [description]
	 * @param  [type] $title  [description]
	 * @param  [type] $html   [description]
	 * @return [type]         [description]
	 */
	public function _sendEmail($client,$cc,$title,$html)
	{
		$this->_createEmailConfig();
		$email_conf = $this->config->item('email');
		$this->email->from($email_conf['username'],$email_conf['name']);
		$this->email->to($client);
		if (!empty($cc)){
			$this->email->cc($cc);
		}
		$this->email->subject($title);
		$this->email->message($html);
		$status=$this->email->send();
		if ($status){
			return true;
		}else{
			print_r($this->email->print_debugger());
			return false;
		}
	}

	/**
	 * 获得SEC事件IP反向查询
	 * @return [type] [description]
	 */
	function ipTOaddress($inputs)
	{
		return file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$inputs["ip"]);
	}
}

?>
